package com.safe.ele.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.json.JSONObject;

/**
 * @program: StartCoding
 * @description:
 * @author: Feri(邢朋辉)
 *
 * 短信验证码的发送
 * @create: 2021-01-06 14:14
 */
public class AliSmsUtil {
    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
    public static String accessKeyId = "LTAI4GFbFXCbP19wDjbdu6dy";
    public static String accessKeySecret = "gpxxP6s5HZMXlsuxFV7spNjBhDjGl4";

    /**
     * 发送短信验证码*/
    public static boolean sendSmsCode(String phone,int code){
        //设置配置对象
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                accessKeyId, accessKeySecret);
        //实例化客户端对象
        IAcsClient client = new DefaultAcsClient(profile);
        //创建请求对象
        CommonRequest request = new CommonRequest();
        //设置请求信息
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "来自邢朋辉的短信");
        request.putQueryParameter("TemplateCode", "SMS_115250125");
        request.putQueryParameter("TemplateParam", "{\"code\":"+code+"}");
        try {
            //发送请求获取响应
            CommonResponse response = client.getCommonResponse(request);
            //验证发送是否成功--这里的依赖json解析的包
            return "OK".equals(new JSONObject(response.getData()).get("Code"));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
